﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebApiV2.Data.Entity;
using WebApiV2.Domain;

namespace WebApiV2.Data
{
    public class DbInitializer
    {
        public static void Seed(IServiceProvider serviceProvider)
        {
            using (var scope = serviceProvider.CreateScope())
            {
                var db = scope.ServiceProvider.GetService(typeof(Admin3400DbContext)) as Admin3400DbContext;

                var _user = scope.ServiceProvider.GetService(typeof(IResponsitory<Users>)) as EfResponsitory<Users>;

                var _role = scope.ServiceProvider.GetService(typeof(IResponsitory<Roles>)) as EfResponsitory<Roles>;

                db.Database.EnsureCreated();

                var hasUser = db.users.Any();  
                if (!hasUser)                   //数据是否存在 存在就不添加
                {
                    var role = new Roles()
                    {
                        RoleName = "管理员",
                    };
                    _role.Insert(role);


                    _user.InsertBulk(new Users[]
                   {
                        new Users
                        {
                            UserName="wwwwww",
                            Password="12345",
                            RolesId=role.Id
                        },
                        new Users
                        {
                            UserName="ssssss",
                            Password="12345",
                            RolesId=role.Id
                        },
                   });



                    var user = new Users()
                    {
                        UserName = "sa",
                        Password = "123456",
                        Roles = role
                    };
                    _user.Insert(user);
                }
            }
        }
        //using var db = new Admin3400DbContext();
    }
}
